package com.syg.boot.mq.cmq.consumer;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author qg
 * @version 1.0
 */
public abstract class CmqMessageHandler<T> {

    /**
     * 消费消息
     * @param messageId
     * @param flowId
     * @param t
     * @throws Exception
     */
    public abstract void onMessage(String messageId, String flowId, T t) throws Exception;

    /**
     * 异常处理
     * @param throwable
     */
    public abstract void onError(Throwable throwable);

    /**
     * 校验泛型类型
     * @return
     */
    public final Type getType() {
        Type superclass = this.getClass().getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new IllegalArgumentException("必须设置MessageHandler中的泛型类型");
        }

        Type type = ((ParameterizedType)superclass).getActualTypeArguments()[0];
        if (type.getTypeName().equals(Object.class.getName())) {
            throw new IllegalArgumentException("MessageHandler中的泛型类型不能是Object");
        }
        return type;
    }
}
